package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl  implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {

        if(list!=null&&list.size()>0){

            for(OrderSetting orderSetting:list){
                System.out.println(orderSetting.getOrderDate());
                Long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                //判断是否存在，存在更新，不存在添加
                if(countByOrderDate>0){
                    orderSettingDao.edit(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }

        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {

        String start=date+"-1";
        String end=date+"-31";
        Map<String, String> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        ArrayList<Map> arrayList = new ArrayList<>();
        if(list !=null && list.size()>0){
            for(OrderSetting orderSetting:list){
                Map<String, Object> map1 = new HashMap<>();
                map1.put("date",orderSetting.getOrderDate().getDate());
                map1.put("number",orderSetting.getNumber());
                map1.put("reservations",orderSetting.getReservations());
                arrayList.add(map1);
            }

        }
        return arrayList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        System.out.println(orderSetting);
        Long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(countByOrderDate>0){
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }

    }
}
